package com.htc.android.footprints.view;

import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.android.maps.GeoPoint;
import com.htc.android.footprints.utils.GpsFixStatus;
import com.htc.android.footprints.utils.GpsFixStatus.FixListener;
import java.util.ArrayList;
import java.util.Iterator;

public class LocationController extends LinearLayout
{
  private static final long FALLBACK_PROVIDER_THRESHOLD_MILLIS = 10000L;
  private static final String GPS = "gps";
  private static final int HANDLER_CHECK_SIGNAL = 102;
  private static final int HANDLER_TRACKING = 101;
  private static final int HANDLER_UPDATE_STATUS = 103;
  private static final int HANDLER_WAIT_TIMEOUT = 100;
  private static final int ICON_LEVEL_GPS_OFF = 6;
  private static final int ICON_LEVEL_GPS_ON = 5;
  private static final int ICON_LEVEL_MAP = 7;
  private static final int ICON_LEVEL_NETWORK = 8;
  private static final int ICON_LEVEL_SIGNAL0 = 0;
  private static final int ICON_LEVEL_SIGNAL1 = 1;
  private static final int ICON_LEVEL_SIGNAL2 = 2;
  private static final int ICON_LEVEL_SIGNAL3 = 3;
  private static final int ICON_LEVEL_SIGNAL4 = 4;
  private static final float LOCATION_MIS_DISTANCE = 0.0F;
  private static final long LOCATION_UPDATE_MILLIS = 0L;
  private static final int LOW_SIGNAL = 5;
  private static final int MODE_FIX_GPS = 12;
  private static final int MODE_FIX_MAP = 15;
  private static final int MODE_FIX_NETWORK = 13;
  private static final int MODE_FIX_NONE = 14;
  private static final int MODE_TRACKING = 11;
  private static final String NETWORK = "network";
  private static String[] PROVIDER_NAMES;
  private static final String TAG = "LocationController";
  private static final long TIMEOUT = 10000L;
  private GpsFixStatus gpsFixStatus = null;
  private GpsStatus.Listener gpsListener = new GpsStatus.Listener()
  {
    GpsStatus gstatus = null;

    public void onGpsStarted()
    {
    }

    public void onGpsStatusChanged(int paramInt)
    {
      if (paramInt == 3)
        if (LocationController.this.mLocation == null)
          Log.d("LocationController", "GpsStatusListener.onFirstFix");
      while (true)
      {
        return;
        if (paramInt == 4)
        {
          this.gstatus = LocationController.this.mLocationManager.getGpsStatus(this.gstatus);
          if (LocationController.this.isTrackingTimeOut)
            continue;
          float f = 0.0F;
          int i = 0;
          Iterator localIterator = this.gstatus.getSatellites().iterator();
          while (localIterator.hasNext())
          {
            f += ((GpsSatellite)localIterator.next()).getSnr();
            i++;
          }
          if (i > 0)
          {
            LocationController.access$902(LocationController.this, f / i);
            LocationController.this.changeGpsSignal();
            continue;
          }
          LocationController.access$902(LocationController.this, 0.0F);
          LocationController.this.changeGpsSignal();
          continue;
        }
      }
    }

    public void onGpsStopped()
    {
    }
  };
  private boolean isFixGps = false;
  private boolean isFixNetwork = false;
  private boolean isTrackingTimeOut = false;
  private ArrayList<Location> locationList = null;
  private ControllerListerner mClientListener = null;
  private Context mContext = null;
  private int mCurMode;
  private final ArrayList<NameAndDate> mEnabledProviders = new ArrayList(2);
  private int mFixAccuracy = 0;
  private float mGpsSignal = 0.0F;
  private Handler mHandler = new Handler()
  {
    public void handleMessage(Message paramMessage)
    {
      switch (paramMessage.what)
      {
      default:
        super.handleMessage(paramMessage);
      case 101:
      case 102:
      case 103:
      case 100:
      }
      while (true)
      {
        return;
        LocationController.this.changeGpsSignal();
        LocationController.this.changeStatus();
        Log.i("LocationController", "HANDLER_TRACKING");
        continue;
        if (LocationController.this.isTrackingTimeOut)
          continue;
        if (LocationController.this.mStatusIcon.getVisibility() == 0)
          LocationController.this.mStatusIcon.setVisibility(4);
        while (true)
        {
          sendEmptyMessageDelayed(102, 1000L);
          break;
          LocationController.this.mStatusIcon.setVisibility(0);
        }
        LocationController.this.changeStatus();
        Log.i("LocationController", "HANDLER_UPDATE_STATUS");
        continue;
        removeMessages(102);
        LocationController.access$802(LocationController.this, true);
        LocationController.access$1302(LocationController.this, 11);
        LocationController.this.changeStatus();
        LocationController.this.removeSystemListener();
        if (LocationController.this.mClientListener != null)
          LocationController.this.mClientListener.onTimeout();
        Log.i("LocationController", "HANDLER_WAIT_TIMEOUT");
      }
    }
  };
  private Location mLocation = null;
  private LocationListener mLocationListener = new LocationListener()
  {
    /** @deprecated */
    public void onLocationChanged(Location paramLocation)
    {
      monitorenter;
      while (true)
      {
        String str;
        try
        {
          long l1 = SystemClock.elapsedRealtime();
          long l2 = l1 - 10000L;
          str = paramLocation.getProvider();
          Iterator localIterator = LocationController.this.mEnabledProviders.iterator();
          if (!localIterator.hasNext())
            continue;
          LocationController.NameAndDate localNameAndDate = (LocationController.NameAndDate)localIterator.next();
          if (!localNameAndDate.name.equals(str))
            continue;
          localNameAndDate.date = l1;
          if (str.equals("gps"))
          {
            LocationController.this.mLocation.set(paramLocation);
            if (LocationController.this.isFixGps)
              continue;
            Log.i("LocationController", "Fix GPS");
            LocationController.access$202(LocationController.this, true);
            LocationController.this.onGpsFix();
            if (LocationController.this.mClientListener == null)
              continue;
            LocationController.this.mClientListener.onUpdateLocation(LocationController.this.mLocation);
            return;
            if ((!localNameAndDate.name.equals("gps")) || (localNameAndDate.date <= l2))
              continue;
            Log.i("LocationController", "Got fallback update soon after preferred udpate, ignoring");
            continue;
          }
        }
        finally
        {
          monitorexit;
        }
        if ((!str.equals("network")) || (LocationController.this.isFixNetwork))
          continue;
        Log.i("LocationController", "FimCurModex Network");
        LocationController.access$502(LocationController.this, true);
        LocationController.access$602(LocationController.this, paramLocation);
      }
    }

    public void onProviderDisabled(String paramString)
    {
    }

    public void onProviderEnabled(String paramString)
    {
    }

    public void onStatusChanged(String paramString, int paramInt, Bundle paramBundle)
    {
    }
  };
  private LocationManager mLocationManager = null;
  private Location mNetworkLocation = null;
  private ImageView mStatusIcon = null;
  private Long mTimeoutTimer = Long.valueOf(10000L);

  static
  {
    String[] arrayOfString = new String[2];
    arrayOfString[0] = "gps";
    arrayOfString[1] = "network";
    PROVIDER_NAMES = arrayOfString;
  }

  public LocationController(Context paramContext)
  {
    super(paramContext);
    this.mContext = paramContext;
    init();
  }

  public LocationController(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.mContext = paramContext;
    init();
  }

  private void averageLocation()
  {
    double d1 = 0.0D;
    double d2 = 0.0D;
    Iterator localIterator = this.locationList.iterator();
    while (localIterator.hasNext())
    {
      Location localLocation = (Location)localIterator.next();
      d1 += localLocation.getLatitude();
      d2 += localLocation.getLongitude();
    }
    double d3 = d1 / this.locationList.size();
    double d4 = d2 / this.locationList.size();
    this.mLocation.setLatitude(d3);
    this.mLocation.setLongitude(d4);
  }

  private void changeGpsSignal()
  {
    if (this.isFixGps);
    while (true)
    {
      return;
      if (this.mGpsSignal < 5.0F)
      {
        this.mStatusIcon.setImageLevel(0);
        continue;
      }
      if ((this.mGpsSignal >= 5.0F) && (this.mGpsSignal < 10.0F))
      {
        this.mStatusIcon.setImageLevel(1);
        continue;
      }
      if ((this.mGpsSignal > 10.0F) && (this.mGpsSignal < 20.0F))
      {
        this.mStatusIcon.setImageLevel(2);
        continue;
      }
      if ((this.mGpsSignal > 20.0F) && (this.mGpsSignal < 30.0F))
      {
        this.mStatusIcon.setImageLevel(3);
        continue;
      }
      if (this.mGpsSignal <= 30.0F)
        continue;
      this.mStatusIcon.setImageLevel(4);
    }
  }

  private void changeStatus()
  {
    this.mStatusIcon.setVisibility(0);
    switch (this.mCurMode)
    {
    default:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    }
    while (true)
    {
      return;
      this.mStatusIcon.setImageLevel(0);
      continue;
      this.mStatusIcon.setImageLevel(5);
      continue;
      this.mStatusIcon.setImageLevel(8);
      continue;
      this.mStatusIcon.setImageLevel(6);
      continue;
      this.mStatusIcon.setImageLevel(7);
    }
  }

  private void checkCurAccuracy()
  {
    if (this.mFixAccuracy == 0)
      this.mCurMode = 14;
    while (true)
    {
      return;
      if (this.mFixAccuracy == 2)
      {
        this.mCurMode = 12;
        continue;
      }
      if (this.mFixAccuracy == 1)
      {
        this.mCurMode = 13;
        continue;
      }
      if (this.mFixAccuracy != 3)
        continue;
      this.mCurMode = 15;
    }
  }

  private void forceChangeStatus()
  {
    removeSystemListener();
    this.isTrackingTimeOut = true;
    this.isFixGps = true;
    this.isFixNetwork = true;
    changeStatus();
  }

  private void init()
  {
    ((LayoutInflater)this.mContext.getSystemService("layout_inflater")).inflate(2130903064, this);
    this.mLocation = new Location("gps");
    this.locationList = new ArrayList();
    this.mStatusIcon = ((ImageView)findViewById(2131492987));
    this.mStatusIcon.setImageLevel(0);
    this.mLocationManager = ((LocationManager)this.mContext.getSystemService("location"));
    resetStatus();
  }

  private void onGpsFix()
  {
    this.mHandler.removeMessages(100);
    this.mHandler.removeMessages(102);
    this.mCurMode = 12;
    changeStatus();
    if (this.mClientListener != null)
      this.mClientListener.onLocationFix(this.mLocation, true);
  }

  private void removeSystemListener()
  {
    this.mLocationManager.removeUpdates(this.mLocationListener);
    this.mLocationManager.removeGpsStatusListener(this.gpsListener);
  }

  private void resetStatus()
  {
    this.isFixGps = false;
    this.isFixNetwork = false;
    this.isTrackingTimeOut = false;
    this.mLocation.reset();
    this.locationList.clear();
    this.mNetworkLocation = null;
    this.mGpsSignal = 0.0F;
    this.mHandler.removeMessages(102);
    this.mHandler.removeMessages(100);
    Iterator localIterator = this.mEnabledProviders.iterator();
    while (localIterator.hasNext())
      ((NameAndDate)localIterator.next()).date = 0L;
  }

  private void setGpsFixListener()
  {
    this.gpsFixStatus.setFixListener(new GpsFixStatus.FixListener()
    {
      public void onFix()
      {
      }
    });
  }

  public void enableMyLocation()
  {
    this.mEnabledProviders.clear();
    removeSystemListener();
    resetStatus();
    this.mCurMode = 11;
    this.mLocationManager.addGpsStatusListener(this.gpsListener);
    String[] arrayOfString = PROVIDER_NAMES;
    int i = arrayOfString.length;
    for (int j = 0; ; j++)
      if (j < i)
      {
        String str = arrayOfString[j];
        try
        {
          if (!this.mLocationManager.isProviderEnabled(str))
            continue;
          this.mEnabledProviders.add(new NameAndDate(str));
          this.mLocationManager.requestLocationUpdates(str, 0L, 0.0F, this.mLocationListener);
          Log.i("LocationController", "Request updates from " + str);
        }
        catch (SecurityException localSecurityException)
        {
          Log.w("LocationController", "Couldn't get provider " + str + ": " + localSecurityException.getMessage());
        }
        catch (IllegalArgumentException localIllegalArgumentException)
        {
          Log.w("LocationController", "Couldn't get provider " + str + ": " + localIllegalArgumentException.getMessage());
        }
      }
      else
      {
        this.mHandler.sendEmptyMessageDelayed(101, 50L);
        this.mHandler.sendEmptyMessageDelayed(100, this.mTimeoutTimer.longValue());
        this.mHandler.sendEmptyMessageDelayed(102, 1000L);
        return;
      }
  }

  public void forceStatusToGPS()
  {
    this.mFixAccuracy = 2;
    this.mCurMode = 12;
    forceChangeStatus();
  }

  public void forceStatusToMap()
  {
    this.mFixAccuracy = 3;
    this.mCurMode = 15;
    forceChangeStatus();
  }

  public void forceStatusToNetwork()
  {
    this.mFixAccuracy = 1;
    this.mCurMode = 13;
    forceChangeStatus();
  }

  public void forceStatusToNoneFix()
  {
    this.mFixAccuracy = 0;
    this.mCurMode = 14;
    forceChangeStatus();
  }

  public int getAccuracy()
  {
    return this.mFixAccuracy;
  }

  public GeoPoint getFixGpsCeoPoint()
  {
    GeoPoint localGeoPoint = null;
    if (this.mLocation != null)
      localGeoPoint = new GeoPoint((int)(1000000.0D * this.mLocation.getLatitude()), (int)(1000000.0D * this.mLocation.getLongitude()));
    return localGeoPoint;
  }

  public Location getFixGpsLocation()
  {
    return this.mLocation;
  }

  public GeoPoint getFixNetworkGeoPoint()
  {
    GeoPoint localGeoPoint = null;
    if (this.mNetworkLocation != null)
      localGeoPoint = new GeoPoint((int)(1000000.0D * this.mNetworkLocation.getLatitude()), (int)(1000000.0D * this.mNetworkLocation.getLongitude()));
    return localGeoPoint;
  }

  public Location getFixNetworkLocation()
  {
    return this.mNetworkLocation;
  }

  public boolean isTrackingGps()
  {
    if (this.mCurMode == 11);
    for (int i = 1; ; i = 0)
      return i;
  }

  public void pauseControl()
  {
    this.mGpsSignal = 0.0F;
    this.isTrackingTimeOut = false;
    this.mHandler.removeMessages(102);
    this.mHandler.removeMessages(100);
    removeSystemListener();
    changeStatus();
  }

  public void pauseControlByAccuracy()
  {
    this.mGpsSignal = 0.0F;
    this.isTrackingTimeOut = false;
    this.mHandler.removeMessages(102);
    this.mHandler.removeMessages(100);
    checkCurAccuracy();
    removeSystemListener();
    changeStatus();
  }

  public void setControllerListener(ControllerListerner paramControllerListerner)
  {
    this.mClientListener = paramControllerListerner;
  }

  public void setTimeoutTimer(Long paramLong)
  {
    this.mTimeoutTimer = paramLong;
  }

  public void shutdownControl()
  {
    this.mCurMode = 14;
    resetStatus();
    removeSystemListener();
    changeStatus();
  }

  private static class NameAndDate
  {
    public long date;
    public String name;

    public NameAndDate(String paramString)
    {
      this.name = paramString;
      this.date = -9223372036854775808L;
    }

    public boolean equals(Object paramObject)
    {
      if (hashCode() == paramObject.hashCode());
      for (int i = 1; ; i = 0)
        return i;
    }

    public int hashCode()
    {
      return this.name.hashCode();
    }
  }

  public static abstract interface ControllerListerner
  {
    public abstract void onLocationFix(Location paramLocation, boolean paramBoolean);

    public abstract void onTimeout();

    public abstract void onUpdateLocation(Location paramLocation);
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.view.LocationController
 * JD-Core Version:    0.6.0
 */